﻿var me = this;

var fuck = PAI.Global.getUrlData('fuck');

if (fuck) {

    btns.push({
        text: 'Communication',
        listeners: {
            click: function () {
                var win = Ext.create('Ext.window.Window', {
                    title: 'Communication',
                    width: 950,
                    height: 600,
                    modal: true,
                    padding: 10,
                    layout: 'fit',
                    cls: 'form-theme landRover',
                    items: {
                        xtype: "paigrid",
                        mobileDisplay: true,
                        pcDisplay: true,
                        PrintDisplay: true,
                        hidden: false,
                        readOnly: false,
                        readOnlyHead: true,
                        name: "datagrid1",
                        editable: false,
                        enableRowStatus: false,
                        enableAdd: false,
                        enablePaging: true,
                        enableActions: false,
                        reference: "datagrid1",
                        enableSearch: false,
                        dockedItems: [{
                            xtype: 'toolbar',
                            dock: 'top',
                            items: {
                                xtype: "paitriggerfield",
                                hidden: false,
                                readOnly: false,
                                fieldLabel: language("Chooese Employee"),
                                questionTip: language(""),
                                name: "ChooeseEmployee",
                                reference: 'ce',
                                reference: "Chooese Employee",
                                editable: false,
                                labelAlign: "left",
                                labelWidth: 120,
                                flex: 1,
                                listeners: {
                                    triggerclick: function (trigger, field, e) {
                                        var ref = me.lookupController().getReferences();
                                        var vm = me.lookupViewModel();

                                        Ext.getBody().mask();
                                        if (!field.win) {
                                            Ext.require([
                                                'PAI.ux.panel.selection.OrgEmpSelectionComp',
                                                'PAI.ux.window.selection.SingleSelectionWin',
                                                'PAI.ux.panel.selection.EmpSelectionComp'
                                            ], function () {
                                                field.win = Ext.create({
                                                    title: 'Select Staff',
                                                    xtype: 'singleselectionwin',
                                                    selectType: 'empselection',
                                                    isMulti: false,
                                                    closeAction: 'hide',
                                                    needOrderByUs: false,
                                                    modal: true,
                                                    listeners: {
                                                        selectionchange: function (win, value) {
                                                            if (!Ext.isEmpty(value)) {
                                                                vm.set('NebulogyForm.secByAgent.UserCDSID', value[0].employeeNo)
                                                                vm.set('NebulogyForm.secByAgent.AgentUserID', value[0].id);
                                                                vm.set('NebulogyForm.secByAgent.UserName', value[0].lastName + " " + value[0].firstName);

                                                                field.setValue(value[0].lastName + " " + value[0].firstName);
                                                            }
                                                        },
                                                        close: function () {
                                                            Ext.getBody().unmask();
                                                        }
                                                    }
                                                }).show()
                                            });
                                        } else {
                                            field.win.show();
                                        };

                                    }
                                }
                            }
                        }, {
                            xtype: 'toolbar',
                            dock: 'bottom',
                            items: {
                                xtype: 'textarea',
                                fieldLabel: 'Comment',
                                flex: 1
                            }
                        }],
                        columns: [{
                            text: language("firstName"),
                            dataIndex: "firstName",
                            minWidth: 100,
                            flex: 1,
                            renderer: function (v, el, r) {
                                return r.get('firstName') + ' ' + r.get('lastName')
                            }
                        }, {
                            text: language("content"),
                            dataIndex: "content",
                            minWidth: 100,
                            flex: 1
                        }],
                        store: {
                            type: 'store',
                            autoLoad: true,
                            fields: [
                                { name: "id" },
                                { name: "procInstId" },
                                { name: "k2ActInstId" },
                                { name: "type" },
                                { name: "destInstId" },
                                { name: "detailId" },
                                { name: "empId" },
                                { name: "lastName" },
                                { name: "firstName" },
                                { name: "content" },
                                { name: "receiveEmpIds" },
                                { name: "partentId" }
                            ],
                            proxy: {
                                type: 'pai.rest',
                                isAll: true,
                                ns: PAI.application.url,
                                requestName: 'page',
                                url: "last/communicationObj",
                                reader: {
                                    rootProperty: "communicationObjList"
                                }
                            },
                        },
                        plugins: {
                            rowexpander: {
                                rowBodyTpl: new Ext.XTemplate(
                                    '<tpl if="content">',
                                    '<div class="formhistory-expansion">',
                                    '{[this.showDetail(values.id)]}',
                                    '</div>',
                                    '</tpl>',
                                    {
                                        showDetail: function (id) {
                                            var html = '',
                                                tpl = '<p><b>Replier: </b>{0}</p>' +
                                                    '<p><b>Reply Date: </b>{1}</p>' +
                                                    '<p><b>Reply content: </b>{2}</p>' +
                                                    '<hr style="height:1px;border:none;border-top:1px solid #a7a7a7;" />'
                                            Ext.PAIRest.request({
                                                ns: PAI.application.url,
                                                url: 'last/communicationDetailObj/page?_dc=' + Date.now(),
                                                method: "POST",
                                                async: false,
                                                data: {
                                                    pId: id
                                                },
                                                success: function (data) {

                                                    Ext.each(data.communicationDetailObjList, function (item) {
                                                        html += Ext.String.format(tpl, item.receiveLastName + ' ' + item.receiveFirstName, Ext.Date.format(item.createdDate, 'Y-m-d'), item.content);
                                                    });

                                                }
                                            });
                                            return html;
                                        }
                                    }
                                )
                            }

                        }
                    },
                    buttons: [{
                        text: 'Confirm',
                        handler: function () {
                            var grid = win.down('grid');
                            grid.setSelection(null)
                        }
                    }]
                }).show();
            }
        }
    });
}